package com.jkgl.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jkgl.common.core.domain.R;
import com.jkgl.common.core.page.PageDomain;
import com.jkgl.common.core.page.PaginationVO;
import com.jkgl.common.utils.PageUtils;
import com.jkgl.common.utils.SecurityUtils;
import com.jkgl.common.utils.bean.BeanUtils;
import com.jkgl.entity.UserInfoDayEntity;
import com.jkgl.model.userinfoday.UserInfoDayPagination;
import com.jkgl.model.userinfoday.UserInfoTodayVO;
import com.jkgl.service.UserInfoDayService;
import com.jkgl.util.DurationUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.jkgl.model.userinfo.UserInfo;
import com.jkgl.model.userinfo.UserInfoPage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jkgl.service.UserInfoSelfService;

import java.time.LocalDate;
import java.util.List;


@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("gather/user")
public class UserInfoController {
    private final UserInfoSelfService selfService;
    private final UserInfoDayService dayService;


    /**
     * @Param type 1日报 2 周报
     */
    @GetMapping("/{type}")
    @ApiOperation("历史报告list")
    public R list(@PathVariable("type")String type, PageDomain pageDomain,String zondDate,String beginTime,String endTime){
        Long userId = SecurityUtils.getUserId();
        log.info("获取历史日报");
        UserInfoDayPagination dayPagination = new UserInfoDayPagination();
        BeanUtils.copyProperties(pageDomain,dayPagination);
        dayPagination.setUserId(userId);
        if(zondDate != null && !zondDate.isEmpty()){
            dayPagination.setStartTime(LocalDate.parse(zondDate));
            dayPagination.setEndTime(LocalDate.parse(zondDate).plusDays(6));
        }
        if(beginTime != null && endTime != null && !beginTime.isEmpty() && !endTime.isEmpty()){
            dayPagination.setStartTime(LocalDate.parse(beginTime));
            dayPagination.setEndTime(LocalDate.parse(endTime));
        }
        IPage<UserInfoDayEntity> result = dayService.getUserList(dayPagination);
        PaginationVO paginationVO = BeanUtil.copyProperties(result, PaginationVO.class);
        List<UserInfoTodayVO> vos = BeanUtil.copyToList(result.getRecords(), UserInfoTodayVO.class);
        if("1".equals(type)){
//            日报
            for(UserInfoTodayVO vo : vos){
                long subTime = DurationUtil.timeDiff(vo.getSleepTime(), vo.getWakeupTime());
                vo.setTimeSubHour((int) (subTime/60));
                vo.setTimeSubMinutes((int) (subTime%60));
            }
            paginationVO.setRecords(vos);
            return R.ok(paginationVO);
        }else if("2".equals(type)){
//            周报
            log.info("获取历史周报");
            paginationVO.setRecords(vos);
            return R.ok(paginationVO);
        }
            return R.fail("参数错误");
    }



}


